﻿using AD.Manage;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Meeting.Entity.CommonEntity;
using XF.Meeting.Entity.dbEntity;
using XF.Meeting.Entity.DTO;
using XF.Meeting.Entity.Query;
using XF.Meeting.Entity.View;
using XF.Meeting.IDal;
using XF.Sugar.IService;


namespace XF.Meeting.Service
{
    public class SettingService : BaseService, ISettingSystemService
    {

        private ISettingDal dal = null;
        private IUserDAL userdal = null;
        private IMeetingDal roomdal = null;
        public SettingService(string Operator, string OperaIP)
        {
            this.Operator = Operator;
            this.OperaIP = OperaIP;
            if (dal == null)
            {
                lock (this.OperaIP)
                {
                    if (dal == null)
                    {
                        dal = DalFactory.DalFactory.GetSettingDaL();
                    }
                }
            }
            if (userdal == null)
            {
                lock (this.OperaIP)
                {
                    if (userdal == null)
                    {
                        userdal = DalFactory.DalFactory.GetUserDAL();
                    }
                }
            }
            if (roomdal == null)
            {
                lock (this.OperaIP)
                {
                    if (roomdal == null)
                    {
                        roomdal = DalFactory.DalFactory.GetMeetingDaL();
                    }
                }
            }
        }
        private readonly int FrequencyLimit = Convert.ToInt32(UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FrequencyLimit"])));

        private UEntity.LogEntity.ELogRecordEntity LogEntity = new UEntity.LogEntity.ELogRecordEntity();

        /// <summary>
        /// 获取系统设置
        /// </summary>
        /// <returns></returns>
        public async Task<List<SystemNameInfo>> GetSystemName()
        {
            List<SystemNameInfo> list = new List<SystemNameInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.SettingService.GetSystemName";
            LogEntity.OperateObject = "GetSystemName";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetSettingSystem(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<SystemNameInfo>(res);
                    list[0].FrequencyLimit = FrequencyLimit;

                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetSystemName Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 获取系统名称
        /// </summary>
        /// <returns></returns>
        public async Task<ADSetting> GetADsetting()
        {
            List<ADSetting> list = new List<ADSetting>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.SettingService.GetADsetting";
            LogEntity.OperateObject = "GetADsetting";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetADsetting(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<ADSetting>(res);
                    

                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetADsetting Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list[0];
        }

        /// <summary>
        /// 获取系统预约显示时段
        /// </summary>
        /// <returns></returns>
        public async Task<SubscribeInfo> GetSubscribe()
        {
            List<SubscribeInfo> list = new List<SubscribeInfo>();
            SubscribeInfo subscribeInfo = new SubscribeInfo();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.SettingService.GetSubscribe";
            LogEntity.OperateObject = "GetSubscribe";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetSubscribe(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<SubscribeInfo>(res);
                    subscribeInfo = list[0];
                    subscribeInfo.SubscribeBeginTime = Convert.ToDateTime("1990-01-01 00:00:00").AddHours(subscribeInfo.SubscribeBegin).ToString("HH:mm:ss");
                    subscribeInfo.SubscribeEndTime = Convert.ToDateTime("1990-01-01 00:00:00").AddHours(subscribeInfo.SubscribeEnd).ToString("HH:mm:ss");
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetSubscribe Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return subscribeInfo;
        }

        /// <summary>
        /// 获取系统设置信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<SettingSystem>> GetSettingSystem()
        {
            List<SettingSystem> list = new List<SettingSystem>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.SettingService.GetSettingSystem";
            LogEntity.OperateObject = "GetSettingSystem";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetSettingSystem(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<SettingSystem>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetSettingSystem Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 获取第三方设置信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<SettingThirdParty>> GetSettingThirdParty()
        {
            List<SettingThirdParty> list = new List<SettingThirdParty>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.SettingService.GetSettingThirdParty";
            LogEntity.OperateObject = "GetSettingThirdParty";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetSettingThirdParty(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<SettingThirdParty>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetSettingThirdParty Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 获取使用许可设置信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<SettingUseLicense>> GetSettingUseLicense()
        {
            List<SettingUseLicense> list = new List<SettingUseLicense>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.SettingService.GetSettingUseLicense";
            LogEntity.OperateObject = "GetSettingUseLicense";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetSettingUseLicense(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<SettingUseLicense>(res);
                    foreach (var item in list)
                    {
                        item.TermOfValidity.ToString("yyyy-MM-dd");
                    }
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetSettingUseLicense Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 修改系统设置信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSettingSystem(SettingSystem entity)
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.UpdateSettingSystem";
            LogEntity.OperateObject = "UpdateSettingSystem";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<SettingSystem>(entity);
            try
            {
                bResult = await dal.UpdateSettingSystem(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("UpdateSettingSystem Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return bResult;
        }

        /// <summary>
        /// 修改第三方设置信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSettingThirdParty(SettingThirdParty entity)
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.UpdateSettingThirdParty";
            LogEntity.OperateObject = "UpdateSettingThirdParty";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<SettingThirdParty>(entity);
            try
            {
                bResult = await dal.UpdateSettingThirdParty(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("UpdateSettingThirdParty Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return bResult;
        }

        /// <summary>
        /// 修改使用许可设置信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateSettingUseLicense(SettingUseLicense entity)
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.UpdateSettingUseLicense";
            LogEntity.OperateObject = "UpdateSettingUseLicense";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<SettingUseLicense>(entity);
            try
            {
                bResult = await dal.UpdateSettingUseLicense(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("UpdateSettingUseLicense Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return bResult;
        }

        /// <summary>
        /// 新增区域信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<(bool, string)> AddRegional(RegionalInfo entity)
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.AddRegional";
            LogEntity.OperateObject = "AddRegional";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RegionalInfo>(entity);
            try
            {
                var list = await GetRegionalByPID(new RegionalQuery()
                {
                    PID = entity.PID
                });
                foreach (var item in list)
                {
                    if (item.RegionalName.Trim() == entity.RegionalName.Trim())
                    {
                        bResult = false;
                        strError = "该区域下存在同名的区域名称";
                        return (bResult, strError);
                    }
                }

                bResult = await dal.AddRegional(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("AddRegional Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return (bResult, strError);
        }

        /// <summary>
        /// 删除区域信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<(bool, string)> DeleteRegional(RegionalInfo entity)
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.DeleteRegional";
            LogEntity.OperateObject = "DeleteRegional";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RegionalInfo>(entity);
            try
            {
                var list = await GetRegionalByPID(new RegionalQuery()
                {
                    PID = entity.ID
                });
                if (list.Count > 0)
                {
                    bResult = false;
                    strError = "该区域下有子区域，无法删除";
                    return (bResult, strError);
                }
                //var userlist = userdal.GetUserInfoListByRID(new UserQuery()
                //{

                //    RID = entity.ID,
                //    pageIndex = 1,
                //    pageSize = 1000000,
                //}, out strError);
                //if (userlist.Result.Item1.Rows.Count > 0)
                //{

                //    bResult = false;
                //    strError = "该区域下有人员信息，无法删除";
                //    return (bResult, strError);
                //}
                var roomlist = roomdal.GetCRoomByRID(new RoomQuery()
                {

                    RID = entity.ID,
                    pageIndex = 1,
                    pageSize = 1000000,
                }, out strError);
                if (roomlist.Result.Item1.Rows.Count > 0)
                {

                    bResult = false;
                    strError = "该区域下有会议室信息，无法删除";
                    return (bResult, strError);
                }
                bResult = await dal.DeleteRegional(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("DeleteRegional Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return (bResult, strError);
        }

        /// <summary>
        /// 修改区域信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<(bool, string)> UpdateRegional(RegionalInfo entity)
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.UpdateRegional";
            LogEntity.OperateObject = "UpdateRegional";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RegionalInfo>(entity);
            try
            {

                var list = await GetRegionalByPID(new RegionalQuery()
                {
                    PID = entity.PID
                });
                
                list.RemoveAll(o => { return o.ID == entity.ID; });//去除AppId=70的项
               
                foreach (var item in list)
                {
                    
                    if (item.RegionalName.Trim() == entity.RegionalName.Trim())
                    {
                        bResult = false;
                        strError = "该区域下存在同名的区域名称";
                        return (bResult, strError);
                    }
                }

                bResult = await dal.UpdateRegional(entity, out strError);
                if (!bResult)
                {
                    LogEntity.OperateResult = "Fail";
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("UpdateRegional Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return (bResult, strError);
        }

        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageList<RegionalInfo>> GetRegional(RegionalQuery query)
        {
            PageList<RegionalInfo> list = new PageList<RegionalInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegional";
            LogEntity.OperateObject = "GetRegional";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = EntityToString.GetEntityToString<RegionalQuery>(query);
            try
            {
                var res = await dal.GetRegional(query, out strError);

                if (strError == "")
                {
                    list.List = TableToList.ToList<RegionalInfo>(res.Item1);
                    list.RecordCount = res.Item2;
                    list.PageCount = res.Item3;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegional Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 获取区域信息   Mobile
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageListMobile<RegionalInfo>> GetRegionalForMobile(RegionalQuery query)
        {

            PageListMobile<RegionalInfo> list = new PageListMobile<RegionalInfo>();
            List<RegionalInfo> mobileList = new List<RegionalInfo>();
            //List<RegionalInfo> newList = new List<RegionalInfo>();
            //List<RegionalInfo> infoList = new List<RegionalInfo>();
            //List<RegionalInfo> listKey = new List<RegionalInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegionalForMobile";
            LogEntity.OperateObject = "GetRegionalForMobile";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
              

                query.PID = 1;
              var  res = await dal.GetRegionalForMobile(query, out strError);
                if (strError == "")
                {

                    mobileList = TableToList.ToList<RegionalInfo>(res.Item1);
                    list.List = mobileList;
                    list.RecordCount = res.Item2;
                    list.PageCount = res.Item3;
                    list.IsHaveNext = res.Item4;
                    //newList = await GetNewList(mobileList);
                    //newList = newList.DistinctBy(I => I.ID).ToList();  //去除重复数据

                    //if (query.Key != "" && query.Key != null)
                    //{
                    //    newList = newList.Where(x => x.RegionalName.Contains(query.Key)).ToList(); //根据关键字过滤
                    //}
                    //if (query.Key!="")
                    //{
                    //    foreach (var item in newList)
                    //    {
                    //        if(item.RegionalName.IndexOf(query.Key) != -1)//根据关键字在list中查询
                    //        {
                    //            listKey.Add(item);
                    //        }
                    //    }
                    //    newList = listKey;
                    //}

                    //if (newList.Count > query.pageIndex * query.pageSize)
                    //{
                    //    list.IsHaveNext = false;
                    //}
                    //else
                    //{
                    //    list.IsHaveNext = true;
                    //}
                    //for (int i = (query.pageIndex - 1) * query.pageSize; i < query.pageIndex * query.pageSize && i < newList.Count; i++)//逻辑分页
                    //{

                    //    infoList.Add(newList[i]);

                    //}
                    //list.List = infoList;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegionalForMobile Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 递归拼接区域  格式：   西安-高新-软件园-汉韵阁    （有重复数据，需要去重）
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<List<RegionalInfo>> GetNewList(List<RegionalInfo> list)
        {
            List<RegionalInfo> newList = new List<RegionalInfo>();
            List<RegionalInfo> newList1 = new List<RegionalInfo>();
            #region gaigai 注释
            //foreach (var item in list)
            //{
            //    RegionalInfo info = new RegionalInfo();
            //    info.ID = item.ID;
            //    info.RegionalName = item.RegionalName;
            //    newList.Add(info);
            //    newList1 = await GetRegionalByPID(new RegionalQuery()//根据父ID获取子区域
            //    {
            //        PID = item.ID
            //    });
            //    foreach (var b in newList1)//循环拼接
            //    {
            //        RegionalInfo info1 = new RegionalInfo();
            //        info1.ID = b.ID;
            //        info1.RegionalName = item.RegionalName + "·" + b.RegionalName;
            //        newList.Add(info1);
            //    }
            //    var c = await GetNewList(newList1);//递归
            //    foreach (var d in c)
            //    {
            //        RegionalInfo info2 = new RegionalInfo();
            //        info2.ID = d.ID;
            //        info2.RegionalName = item.RegionalName + "·" + d.RegionalName;//再次循环拼接
            //        newList.Add(info2);
            //    }
            //}
            #endregion
            return newList;
        }

        /// <summary>
        /// 获取区域根节点信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<RegionalInfo>> GetRegionalOne()
        {
            List<RegionalInfo> list = new List<RegionalInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegionalOne";
            LogEntity.OperateObject = "GetRegionalOne";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetRegionalOne(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<RegionalInfo>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegionalOne Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 获取区域根节点信息(管理员)
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<RegionalInfo>> GetRegionalOneForAdmin(RegionalInfo query)
        {
            List<RegionalInfo> list = new List<RegionalInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegionalOne";
            LogEntity.OperateObject = "GetRegionalOne";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetRegionalOneForAdmin(query, out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<RegionalInfo>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegionalOne Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 根据父ID获取区域信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<RegionalInfo>> GetRegionalByPID(RegionalQuery query)
        {
            List<RegionalInfo> list = new List<RegionalInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegionalByPID";
            LogEntity.OperateObject = "GetRegionalByPID";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetRegionalByPID(query, out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<RegionalInfo>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegionalByPID Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// Pad(区域会议室)
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<Root>> GetRegionalForPad()
        {


            List<Root> list = new List<Root>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegionalByPID";
            LogEntity.OperateObject = "GetRegionalByPID";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res1 = await dal.GetRegionalForPad(out strError);
                if (strError == "")
                {
                    var list1 = TableToList.ToList<RegionalInfo>(res1);
                    List<Root> listRoot = new List<Root>();
                    if(list1.Count>0&& list1!=null)
                    {
                        foreach (var item1 in list1)
                        {
                            Root root = new Root();
                            List<SubArea> sub = new List<SubArea>();
                            root.name = item1.RegionalName;
                            list.Add(root);
                            var res2 = await dal.GetRegionalByPathForPad(new RegionalInfo()
                            {
                                Path = item1.ID.ToString(),
                            }, out strError);
                            var list2 = TableToList.ToList<RegionalInfo>(res2);
                            if (list2.Count > 0 && list2 != null)
                            {
                                foreach (var item2 in list2)
                                {

                                    SubArea area = new SubArea();
                                    List<MeetingRoom> rooms = new List<MeetingRoom>();
                                    area.name = item2.RegionalName;
                                    sub.Add(area);
                                    var res3 = await dal.GetCRoomNameByRid(new RoomQuery()
                                    {
                                        RID = item2.ID,
                                    }, out strError);
                                    var list3 = TableToList.ToList<CRoomInfo>(res3);

                                    if(list3.Count > 0 && list3 != null)
                                    {
                                        foreach (var item3 in list3)
                                        {
                                            MeetingRoom room = new MeetingRoom();
                                            room.ID = item3.ID;
                                            room.name = item3.ConferenceName;
                                            rooms.Add(room);
                                            area.sub = rooms;
                                        }
                                        root.sub = sub;
                                    }
                                    
                                }
                            }
                               

                            listRoot.Add(root);
                        }
                    }
                   
                    

                    list = listRoot;



                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegionalForPad Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// AD账号同步
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> ADUserSync()
        {
            string strError = "";
            bool bResult = true;
            LogEntity.Module = "XF.Meeting.Service.MeetingService.ADUserSync";
            LogEntity.OperateObject = "ADUserSync";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                ADSync adsync = new ADSync();
                //bResult =await adsync.ADSyncManage();
                //if (!bResult)
                //{
                //    LogEntity.OperateResult = "Fail";
                //    bResult = false;
                //}
            }
            catch (Exception ex)
            {
                bResult = false;
                LogEntity.OperateResult = "Fail";
                strError = string.Format("ADUserSync Exception:{0}", ex.ToString());

            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return bResult;
        }

        /// <summary>
        /// 行政组织树
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<OrganizationTree>> GetOrganizationTree()
        {
            List<OrganizationTree> list = new List<OrganizationTree>();
            List<OrganizationTree> data = new List<OrganizationTree>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetOrganizationTree";
            LogEntity.OperateObject = "GetOrganizationTree";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetOrganizationTree(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<OrganizationTree>(res);
                    
                    data.AddRange(list.Where(p => p.PID == 0).ToList());//将一级数据加入data中
                    foreach (var item in data)
                    {
                        AddProjectChildren(item, list);
                    }
                    return data;
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetOrganizationTree Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return data;
        }

        /// <summary>
        /// 行政组织树（递归方法）
        /// </summary>
        /// <param name="model"></param>
        /// <param name="typeList"></param>
        /// <returns></returns>
        private OrganizationTree AddProjectChildren(OrganizationTree model, List<OrganizationTree> typeList)
        {
            var treeList = new List<OrganizationTree>();
            var list = typeList.Where(p => p.PID == model.id).ToList();
            if (list.Count == 0) return null;
            foreach (var item in list)
            {
                treeList.Add(item);
                typeList.Remove(item);
            }
            
            model.children = treeList.Count>0? treeList:null;
            foreach (var children in model.children)
            {
                AddProjectChildren(children, typeList);
            }
            return model;
        }

        

        /// <summary>
        /// 获取区域下的关联组织
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<Regional_OrganizationEntity>> GetRegional_Organization(Regional_Organization query)
        {
            List<Regional_OrganizationEntity> list = new List<Regional_OrganizationEntity>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetRegional_Organization";
            LogEntity.OperateObject = "GetRegional_Organization";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetRegional_Organization(query, out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<Regional_OrganizationEntity>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetRegional_Organization Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

       

        /// <summary>
        /// 获取行政组织根节点信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<OrganizationInfo>> GetOrganizationOne()
        {
            List<OrganizationInfo> list = new List<OrganizationInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.OrganizationOne";
            LogEntity.OperateObject = "OrganizationOne";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetOrganizationOne(out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<OrganizationInfo>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("OrganizationOne Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 根据父ID获取行政组织信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<OrganizationInfo>> GetOrganizationByPID(OrganizationQuery query)
        {
            List<OrganizationInfo> list = new List<OrganizationInfo>();
            string strError = "";
            LogEntity.Module = "XF.Meeting.Service.MeetingService.GetOrganizationByPID";
            LogEntity.OperateObject = "GetOrganizationByPID";
            LogEntity.OperateResult = "Success";
            LogEntity.OperationDescription = "";
            try
            {
                var res = await dal.GetOrganizationByPID(query, out strError);

                if (strError == "")
                {
                    list = TableToList.ToList<OrganizationInfo>(res);
                }
                else
                {
                    LogEntity.OperateResult = "Fail";
                }
            }
            catch (Exception ex)
            {
                LogEntity.OperateResult = "Fail";
                strError = string.Format("GetOrganizationByPID Exception:{0}", ex.ToString());
            }
            LogEntity.ErrorDescription = strError;
            RecordLog();
            return list;
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        private void RecordLog()
        {
            LogEntity.Operator = Operator;
            LogEntity.OperateIP = OperaIP;
            ULogWork.CLogToDataBase.RecordOperateLog(LogEntity);
        }


    }
}